package com.rongke.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.rongke.mapper.AdminMapper;
import com.rongke.model.Admin;
import com.rongke.service.AdminService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @AdminServiceImpl
 * @后台登录ServiceImpl
 * @version : Ver 1.0
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    /**
     * @根据条件查找后台登录
     * @param admin
     * @return
     */
    @Override
    public List<Admin> selectByCondition(Admin admin) {
        return adminMapper.selectByCondition(admin);
    }

    /**
     * 获取当前登陆用户
     * @return
     */
    public Admin currentUser() {
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection collection = subject.getPrincipals();
        if (null != collection && !collection.isEmpty()) {
            String userName = (String) collection.iterator().next();
            Admin admin=new Admin();
            admin.setLoginName(userName);
            return adminMapper.selectByCondition(admin).get(0);
        }else{
            return null;
        }
    }

    @Override
    public void logout() {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            // session 会销毁，在SessionListener监听session销毁，清理权限缓存
            subject.logout();
        }

    }

    @Override
    public List<Map> getAdminList() {
        return adminMapper.getAdminList();
    }

    @Override
    public List<Map> selectJurisdictionList(Map condtion) {
        return adminMapper.selectJurisdictionList(condtion);
    }

}
